<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<meta name="author" content="wangzhichao">
	<title>商品组管理</title>
	<script type="text/javascript" language="JavaScript" src="../script/common.js"></script>
<script type="text/javascript" language="JavaScript">
	var qt = null;
	var gqt = null;
	var goodsMap = {
		dataMap : {},
		adds : {},
		dels : {}
	}
	function load(){
		Eui.showNavigation();
		setPages(["listview","detview","detviewdiv"]);
		createTable();
	}
	function createTable(){
		var columns = [];
		columns[0]={type:"checkbox"} 
		columns[1]={render:function(v,r){
			return "<a href=# onclick=\"onView("+r[0]+");return false;\">"+v+"</a>";	
		}}
		columns[6]={title:"操作",width:"15%",render:function(v,r){
			var str = ""
			str += "<a href=# onclick='onEdit("+r[0]+")'>编辑</a>"
			return str
		},align:"center",order:false}
		qt = new QueryTable("goodsgroup_query","searchForm","datalist",columns,{
			buttons:[
				{value:"新增组",actions:onNew},
				{value:"删除组",actions:onDelete}
			]
		});
	}
	function onNew(){
		goodsGroupForm.reset();
		switchPage("detview");
		createGoodsQt()
		gqt.dataSrc = []
		showGoodsQt()
	}
	function showGoodsQt(){
		gqt.showTable()
		goodsMap.dataMap= {} 
		for (var i=0;i<gqt.dataSrc.objects.length;i++){
			var s = gqt.dataSrc.objects[i]
			goodsMap.dataMap[s[0]] = s
		}
	}
	function onDelete(){
		var ids = qt.getBoxValue();
		if(!ids){showInformation("请选择要删除的商品组");return;}
		if (!showQuestion("是否删除这些商品组？")) return;
		DWR.call("goodsGroupService.deleteGoodsGroups",ids,function(){
			showInformation("商品组删除成功");
			qt.showTable();
		},{blockScreen:true})
	}
	function showData(){
		qt.showTable();
	}
	function onEdit(id){
		DWR.call("goodsGroupService.getGoodsGroup",id,function(data){
			var group = data.group;
			var goods = data.goods;
			Common.setValues("goodsGroupForm",group)
			switchPage("detview");
			createGoodsQt()
			gqt.dataSrc = goods
			showGoodsQt()
		},{blockScreen:true})
	}
	
	function onView(id){
		DWR.call("goodsGroupService.getGoodsGroup",id,function(data){
			var group = data.group;
			var goods = data.goods;
			var strs = "";
			for (var i=0;i<goods.length;i++){
				var s = goods[i];
				if (strs) strs += ",";
				strs += s[2];
			}
			$("goodsGroupName").innerHTML = group.name;
			$("goodsGroupUniqueId").innerHTML = group.uniqueId;
			$("goodsNames").innerHTML = strs;
			switchPage("detviewdiv");
		},{blockScreen:true})
	}
	
	function createGoodsQt(){
		if (!gqt){
			var columns = [];
			columns[0]={title : "ID",type:"checkbox"} 
			columns[1]={title:"商品编号"} 
			columns[2]={title:"商品名称"}
			gqt = new QueryTable(null,null,"goodsgroupdiv",columns,{
				title:"商品列表",
				columnsConf:false,
				buttons:[
					{value:"添加商品",actions:addGoods},
					{value:"删除商品",actions:delGoods}
				]
			});
		}
	}
	function showGoodsQt(){
		gqt.showTable()
		goodsMap.dataMap= {} 
		for (var i=0;i<gqt.dataSrc.objects.length;i++){
			var s = gqt.dataSrc.objects[i]
			goodsMap.dataMap[s[0]] = s
		}
	}
	function addGoods(){
		var selectedCode = "";
		for (p in goodsMap.dataMap){
			var c = goodsMap.dataMap[p];
			if (typeof c == "function") continue;
			if (selectedCode) selectedCode += ",";
			selectedCode += c[0];
		}
		CardMgnt.selFuel(function(ss){
			var arrs = gqt.dataSrc.objects
			for (var i=0;i<ss.length;i++){
				var s = ss[i];
				if (!goodsMap.dataMap[s.id]){
					arrs.push([s.id,s.uniqueId,s.name])
					if (!goodsMap.dels[s.id])
						goodsMap.adds[s.id] = s
					else
						goodsMap.dels[s.id] = null
				}
			}
			gqt.dataSrc = arrs;
			showGoodsQt();
		},{
			selectedCode : selectedCode
		})
	}
	function delGoods(){
		var ids = gqt.getBoxValue();
		if (!ids) {showWarning("请选择一个商品！");return;};
		var ids = "," + ids + ",";
		var arrs = gqt.dataSrc.objects;
		var dataSrc = []
		for (var i=0;i<arrs.length;i++){
			var s = arrs[i];
			if (ids.indexOf(","+s[0]+",") ==-1 )
				dataSrc.push(s);
			else{
				if (!goodsMap.adds[s[0]])
					goodsMap.dels[s[0]] = s
				else
					goodsMap.adds[s[0]] = null
			}
		}
		gqt.dataSrc = dataSrc
		showGoodsQt()
	}
	function saveGoodsGroup(){
		if (!checkForm(goodsGroupForm)) return;
		var ggObj = Common.getValues("goodsGroupForm");
		var adds = toArray(goodsMap.adds)
		var dels = toArray(goodsMap.dels)
		DWR.call("goodsGroupService.saveGoodsGroup",ggObj,adds,dels,function(){
			showInformation("商品组"+ggObj.name+"保存成功");
			back()
		},{blockScreen:true})
	}
	function toArray(map){
		if (typeof map != "object") return [];
		var arrs = []
		for (prop in map){
			var c = map[prop]
			if (!c || typeof c == "function") continue;
			arrs.push(prop)
		}
		return arrs;
	}
	function back(){
		goodsMap = {dataMap : {},adds : {},dels : {}}
		switchPage("listview");
		qt.showTable();
	}
</script>
</head>
	
<body onload="load()" class="noScroll">
<!--页面导航-->
<div class="nav"><div id="nav">&nbsp;</div></div>
<!--查询静态页面部分-->
<div id="listview"><!--查询条件form-->
<div class="searchForm">
<form id="searchForm" name="searchForm">
<table cellSpacing="0" cellPadding="4" width="100%" border="0">
	<tr>
		<td><label>商品组名称</label> <input type="text" name="name" maxlength="32"></td>
		<td><label>商品组编号</label> <input type="text" name="uniqueId" maxlength="5"></td>
		<td>
		<button type="button" onclick="showData()">查 询</button>
		</td>
	</tr>
</table>
</form>
</div>
<div id=datalist></div>
</div>
<div id="detview" style="display: none;">
<div class="form">
<div class="formTitle">商品组信息</div>
<form id="goodsGroupForm" name="goodsGroupForm">
	<input type="hidden" name="id">
	<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label6">
		<tr>
			<td><label>商品组名称</label> <input type="text" name="name" maxlength="32" onblur="checkV(this,'',true)"> <span class="des">*</span></td>
		</tr>
	</table>
</form>
</div>
<div id="goodsgroupdiv"></div>
<p>
	<button onclick="saveGoodsGroup()">保 存</button>
	<button onclick="back()">返 回</button>
</p>
</div>
<div id="detviewdiv" style="display: none;">
	<div class="form">
		<div class="formTitle">商品组信息</div>
		<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="viewTable">
			<tr>
				<td><label>商品组名称</label> <span id="goodsGroupName"></span></td>
				<td><label>商品组编号</label> <span id="goodsGroupUniqueId"></span></td>
			</tr>
			<tr>
				<td colspan="2"><label>商品信息</label> <span id="goodsNames"></span></td>
			</tr>
		</table>
	</div>
	<p>
		<button onclick="switchPage('listview');">返 回</button>
	</p>
</div>
</body>
</html>